package org.megha.blog.example.part14;

import java.util.HashMap;
import java.util.Map;

/**
 * A Singleton that holds pricing data for various shipping modes.
 */
public class ShippingPrice {
	private static final ShippingPrice singleton = new ShippingPrice();

	private Map<ShippingMode, Double> priceMap;

	private ShippingPrice() {
		priceMap = new HashMap<ShippingMode, Double>();
		priceMap.put(ShippingMode.GROUND, 1.0);
		priceMap.put(ShippingMode.EXPRESS, 5.5);
		priceMap.put(ShippingMode.OVERNIGHT, 15.0);
	}

	public double getPrice(ShippingMode mode) {
		return priceMap.get(mode);
	}

	public static ShippingPrice getSingleton() {
		return singleton;
	}

	public static void main(String args[]) {
		ShippingPrice shippingPrice = ShippingPrice.getSingleton();

		for (ShippingMode mode : ShippingMode.values()) {
			System.out.println("mode : " + mode + " / price : $" + shippingPrice.getPrice(mode));
		}

		String stringFromEnum = ShippingMode.EXPRESS.toString();
		// string is "EXPRESS"

		// getting the enum for a string
		for (ShippingMode mode : ShippingMode.values()) {
			if (mode.toString().equals(stringFromEnum)) {
				// match found
			}
		}
		// this way is much faster/cleaner
		ShippingMode enumFromString = ShippingMode.valueOf(stringFromEnum);
	}
}
